package com.boardour.toupin.push;

import android.content.Intent;
import android.media.projection.MediaProjection;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.boardour.base.ui.AppActivity;
import com.boardour.base.ui.titlebar.DefTitleBar;
import com.boardour.socket.client.WifiClientPresenter;
import com.boardour.toupin.R;
import com.boardour.toupin.WifiConnectUtils;
import com.boardour.wifip2p.WifiP2pCommon;

import java.net.InetAddress;

/**
 * 录屏发送端
 */
public class PushActivity extends AppActivity implements ProjectionObserver, WifiConnectUtils.OnWifiConnectListener {

    private ProjectionUtils projectionUtils;
    private WifiConnectUtils wifiConnectUtils;
    private PushEncode pushEncode;
    private TextView tvWifi, tvInfo;
    private Handler handler = new Handler(Looper.getMainLooper());
    private Runnable runnable;
    private WifiClientPresenter client = new WifiClientPresenter();

    @Override
    protected void initTitle(DefTitleBar titleBar) {
        titleBar.setTitle("录屏发送端");
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_push;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        projectionUtils = new ProjectionUtils();
        wifiConnectUtils = new WifiConnectUtils(this);
        ProjectionObservable.getInstance().register(this);
        wifiConnectUtils.setOnWifiConnectListener(this);
    }

    @Override
    public void initViews(View view) {
        tvWifi = findViewById(R.id.tv_wifi);
        tvInfo = findViewById(R.id.tv_info);
        requestConnectInfo();
        runnable = new Runnable() {
            @Override
            public void run() {
                long time = System.currentTimeMillis();
                CharSequence sysTimeStr = DateFormat.format("hh:mm:ss", time);//时间显示格式
                tvWifi.setText(sysTimeStr + "\n" + time);
                handler.postDelayed(runnable, 100);
            }
        };
        handler.postDelayed(runnable, 100);
    }


    @Override
    public void onWifiConnect(boolean isConnect) {
        if (isConnect) {
            tvInfo.postDelayed(new Runnable() {
                @Override
                public void run() {
                    requestConnectInfo();
                }
            }, 500);
        }
    }

    private void requestConnectInfo() {
        // 如果没有连接
        WifiP2pCommon.getInstance().requestConnectionInfo(new WifiP2pManager.ConnectionInfoListener() {
            @Override
            public void onConnectionInfoAvailable(WifiP2pInfo info) {
                InetAddress address = info.groupOwnerAddress;
                if (address == null) {
                    return;
                }
                tvInfo.setText(address.toString() + "\r\n" + info.isGroupOwner);
                projectionUtils.start(PushActivity.this);
            }
        });
    }

    public void wifi(View view) {
        wifiConnectUtils.showPop();
    }

    public void start(View view) {
        requestConnectInfo();
    }

    public void stop(View view) {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        projectionUtils.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onChange(MediaProjection mediaProjection) {
        pushEncode = new PushEncode(mediaProjection, client);
        pushEncode.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ProjectionObservable.getInstance().unregister(this);
        WifiP2pCommon.getInstance().removeGroup();
        if (pushEncode != null) {
            pushEncode.close();
            pushEncode = null;
        }
        if (projectionUtils != null) {
            projectionUtils.stop();
        }
        if (client != null) {
            client.close();
        }
        handler.removeCallbacks(runnable);
    }
}
